package com.example.administrator.shzs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.entity.OrderAll;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;


import java.util.List;

import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 订单页面  --- 全部订单
 * Created by Administrator on 2016/10/29.
 */

public class AllOrderAdapter extends RecyclerView.Adapter<AllOrderAdapter.MyViewHoder> {


    private List<OrderAll> datalist;

    private Context context;

    public AllOrderAdapter(List<OrderAll> datalist, Context context) {
        this.datalist = datalist;
        this.context = context;
    }


    public interface OrdelAlll {
        void Onclick(View view, int postion, int po,String shopImage);

    }

    private OrdelAlll ordelAlll;

    public void setOrdelAlll(OrdelAlll listent) {
        this.ordelAlll = listent;
    }


    @Override
    public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.xrecyclerview_all_order_item, null);

        MyViewHoder hoder = new MyViewHoder(view);

        return hoder;
    }

    @Override
    public void onBindViewHolder(final MyViewHoder holder, final int position) {

        final String shopImage=datalist.get(position).getIcon();
        holder.tvMoney.setText(datalist.get(position).getTotalPrice());

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.user)
                .showImageForEmptyUri(R.mipmap.user)
                .showImageOnFail(R.mipmap.user)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();


        ImageLoader.getInstance().displayImage(shopImage,holder.ivShopImage,options);

        holder.tvOnMoreOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ordelAlll.Onclick(holder.tvOnMoreOrder, position, 0,shopImage);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ordelAlll.Onclick(holder.itemView, position, 1,shopImage);
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ordelAlll.Onclick(holder.itemView, position, 2,shopImage);
                return true;
            }
        });


        //保存删除是否成功
        final OrderAll item = datalist.get(position);
        holder.checkBox.setChecked(item.isclick);
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.isclick = holder.checkBox.isChecked();
                ordelAlll.Onclick(holder.checkBox,position,3,shopImage);
            }
        });




        holder.tvShopName.setText(datalist.get(position).getTogoName());


    }


    @Override
    public int getItemCount() {
        return datalist == null ? 0 : datalist.size();
    }

    public class MyViewHoder extends RecyclerView.ViewHolder {

        private TextView tvShopName;//商家名
        private TextView tvCommodity;//商品名称
        private TextView tvNumber;//商品数量 x1
        private TextView tvTotalNumber;//总数量
        private TextView tvMoney;//金额

        private TextView tvCancleOrder;//取消订单
        private TextView tvOnMoreOrder;//再来一单
        private CircleImageView ivShopImage;//商家图片


        private CheckBox checkBox;


        public MyViewHoder(View itemView) {
            super(itemView);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);

//            mSwipeSwitch= itemView.findViewById(R.id.content_view);
            tvNumber = (TextView) itemView.findViewById(R.id.tv_number_all_order);
            tvShopName = (TextView) itemView.findViewById(R.id.tv_shopName_all_order);
            tvCommodity = (TextView) itemView.findViewById(R.id.tv_commodity_all_order_item);
            tvTotalNumber = (TextView) itemView.findViewById(R.id.tv_Toatalnumber_all_order);
            tvMoney = (TextView) itemView.findViewById(R.id.tv_money);
            ivShopImage = (CircleImageView) itemView.findViewById(R.id.iv_shopImage_all_order_item);

            tvCancleOrder = (TextView) itemView.findViewById(R.id.tv_cancle_order);
            tvOnMoreOrder = (TextView) itemView.findViewById(R.id.tv_one_more_order);
        }
    }

    public void remove(OrderAll morderAll) {
        datalist.remove(morderAll);
//        notifyDataSetChanged();
//        if (datalist.size() == 0) {
//            Toast.makeText(context, "已经没数据啦", Toast.LENGTH_SHORT).show();
//        }

    }

    public OrderAll getItem(int pos) {
        return datalist.get(pos);
    }

}
